package com.bifang.common.util;

import cn.hutool.core.bean.BeanUtil;
import com.bifang.common.exception.AppException;
import com.bifang.common.vo.OplogInfo;
import java.util.ArrayList;
import java.util.List;
import org.springframework.util.CollectionUtils;

/**
 * 日志 工具
 *
 * @author chenzh
 * @date 2020-09-44
 */
public class OplogUtil {
    private static final ThreadLocal<List<OplogInfo>> oplogThreadLocal =
            new ThreadLocal<List<OplogInfo>>() {
                @Override
                protected List<OplogInfo> initialValue() {
                    return new ArrayList<>();
                }
            };

    public OplogUtil() {}

    public static void insert(OplogInfo oplogInfo) {
        List<OplogInfo> list = oplogThreadLocal.get();
        if (CollectionUtils.isEmpty(list)) {
            list = new ArrayList<>();
        }
        list.add(oplogInfo);
        oplogThreadLocal.set(list);
    }

    public static void del(OplogInfo oplogInfo) {
        List<OplogInfo> list = oplogThreadLocal.get();
        if (CollectionUtils.isEmpty(list)) {
            list = new ArrayList<>();
        } else {
            int size = list.size();
            if (oplogInfo == null) {
                list.remove(size - 1);
            } else {
                if (oplogInfo.getOplogId() == null) {
                    list.remove(size - 1);
                } else {
                    boolean flag = true;
                    for (int i = 0; i < size; i++) {
                        OplogInfo old = list.get(i);
                        if (old.getOplogId() != null
                                && old.getOplogId() - oplogInfo.getOplogId() == 0) {
                            list.remove(i);
                            flag = false;
                        }
                    }
                    if (flag) {
                        list.remove(size - 1);
                    }
                }
            }
        }
        oplogThreadLocal.set(list);
    }

    public static void update(OplogInfo oplogInfo) {
        List<OplogInfo> list = oplogThreadLocal.get();
        if (CollectionUtils.isEmpty(list)) {
            throw new AppException("没有需要更新的数据");
        } else {
            int size = list.size();
            if (oplogInfo == null) {
                throw new AppException("不能更新为null，请用删除");
            } else {
                if (oplogInfo.getOplogId() == null) {
                    throw new AppException("不能更新oplogId为null，请用删除");
                } else {
                    boolean flag = true;
                    for (OplogInfo old : list) {
                        if (old.getOplogId() != null
                                && old.getOplogId() - oplogInfo.getOplogId() == 0) {
                            BeanUtil.copyProperties(oplogInfo, old);
                            flag = false;
                        }
                    }
                    if (!flag) {
                        list.remove(size - 1);
                        list.add(oplogInfo);
                    }
                }
            }
        }
        oplogThreadLocal.set(list);
    }

    public static void setOplogInfo(OplogInfo oplogInfo) {}

    public static OplogInfo getOplogInfo() {
        List<OplogInfo> list = oplogThreadLocal.get();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        } else {
            return list.get(list.size() - 1);
        }
    }

    public static void removeOplogInfo() {
        oplogThreadLocal.remove();
    }

    public static Long getOplogId() {
        OplogInfo oplogInfo = getOplogInfo();
        return oplogInfo == null ? null : oplogInfo.getOplogId();
    }

    public static String getLogFlag() {
        OplogInfo oplogInfo = getOplogInfo();
        return oplogInfo == null ? null : oplogInfo.getLogFlag();
    }
}
